package megrez.keycloak.providers.password;

import org.keycloak.Config.Scope;
import org.keycloak.credential.hash.PasswordHashProvider;
import org.keycloak.credential.hash.PasswordHashProviderFactory;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * 自定义加密方法
 * 
 * @author Lucky Yang
 * @since 17.0.1
 */
public class CustomPasswordHashProviderFactory implements PasswordHashProviderFactory {
    public static final String PROVIDER_ID = "custom-password-hash";
    private CustomPasswordHashProvider provider;

    @Override
    public PasswordHashProvider create(KeycloakSession session) {
        return provider;
    }

    @Override
    public void init(Scope config) {
        PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
        provider = new CustomPasswordHashProvider(passwordEncoder);
    }

    @Override
    public void postInit(KeycloakSessionFactory factory) {
        // 不需要实现
    }

    @Override
    public void close() {
        // 不需要实现
    }

    @Override
    public String getId() {
        return PROVIDER_ID;
    }

}
